home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / cfortune.zip / UNSTR.C < prev    next >
C/C++ Source or Header  |  1989-05-25  |  3KB  |  128 lines

  1. # include    <stdio.h>
  2. # include    "strfile.h"
  3.  
  4. # define    TRUE    1
  5. # define    FALSE    0
  6.  
  7. /*
  8.  *    This program un-does what "strfile" makes, thereby obtaining the
  9.  * original file again.  This can be invoked with the name of the output
  10.  * file, the input file, or both. If invoked with only a single argument
  11.  * ending in ".dat", it is pressumed to be the input file and the output
  12.  * file will be the same stripped of the ".dat".  If the single argument
  13.  * doesn't end in ".dat", then it is presumed to be the output file, and
  14.  * the input file is that name prepended by a ".dat".  If both are given
  15.  * they are treated literally as the input and output files.
  16.  *
  17.  *    Ken Arnold        Aug 13, 1978
  18.  */
  19.  
  20. # define    DELIM_CH    '-'
  21.  
  22. char    Infile[100],            /* name of input file */
  23.     Outfile[100];            /* name of output file */
  24.  
  25. short    Oflag = FALSE;            /* use order of initial table */
  26.  
  27. FILE    *Inf, *Outf;
  28.  
  29. char    *rindex(), *malloc(), *strcat(), *strcpy();
  30.  
  31. main(ac, av)
  32. int    ac;
  33. char    **av;
  34. {
  35.     register char    c;
  36.     register int    nstr, delim;
  37.     static STRFILE    tbl;        /* description table */
  38.  
  39.     getargs(ac, av);
  40.     if ((Inf = fopen(Infile, "r")) == NULL) {
  41.         perror(Infile);
  42.         exit(-1);
  43.         /* NOTREACHED */
  44.     }
  45.     if ((Outf = fopen(Outfile, "w")) == NULL) {
  46.         perror(Outfile);
  47.         exit(-1);
  48.         /* NOTREACHED */
  49.     }
  50.     (void) fread((char *) &tbl, sizeof tbl, 1, Inf);
  51.     if (Oflag) {
  52.         order_unstr(&tbl);
  53.         exit(0);
  54.         /* NOTREACHED */
  55.     }
  56.     nstr = tbl.str_numstr;
  57.     (void) fseek(Inf, (long) (sizeof (long) * (nstr + 1)), 1);
  58.     delim = 0;
  59.     for (nstr = 0; (c = getc(Inf)) != EOF; nstr++)
  60.         if (c != '\0')
  61.             putc(c, Outf);
  62.         else if (nstr != tbl.str_numstr - 1)
  63.             if (nstr == tbl.str_delims[delim]) {
  64.                 fputs("%-\n", Outf);
  65.                 delim++;
  66.             }
  67.             else
  68.                 fputs("%%\n", Outf);
  69.     exit(0);
  70.     /* NOTREACHED */
  71. }
  72.  
  73. getargs(ac, av)
  74. register int    ac;
  75. register char    **av;
  76. {
  77.     register char    *sp;
  78.  
  79.     if (ac > 1 && strcmp(av[1], "-o") == 0) {
  80.         Oflag++;
  81.         ac--;
  82.         av++;
  83.     }
  84.     if (ac < 2) {
  85.         printf("usage: %s datafile[.dat] [ outfile ]\n", av[0]);
  86.         exit(-1);
  87.     }
  88.     (void) strcpy(Infile, av[1]);
  89.     if (ac < 3) {
  90.         (void) strcpy(Outfile, Infile);
  91.         if ((sp = rindex(av[1], '.')) && strcmp(sp, ".dat") == 0)
  92.             Outfile[strlen(Outfile) - 4] = '\0';
  93.         else
  94.             (void) strcat(Infile, ".dat");
  95.     }
  96.     else
  97.         (void) strcpy(Outfile, av[2]);
  98. }
  99.  
  100. order_unstr(tbl)
  101. STRFILE    *tbl;
  102. {
  103.     register int    i, c;
  104.     register int    delim;
  105.     register long    *seekpts;
  106.  
  107.     seekpts = (long *) malloc(sizeof *seekpts * tbl->str_numstr);    /* NOSTRICT */
  108.     if (seekpts == NULL) {
  109.         perror("malloc");
  110.         exit(-1);
  111.         /* NOTREACHED */
  112.     }
  113.     (void) fread((char *) seekpts, sizeof *seekpts, tbl->str_numstr, Inf);
  114.     delim = 0;
  115.     for (i = 0; i < tbl->str_numstr; i++, seekpts++) {
  116.         if (i != 0)
  117.             if (i == tbl->str_delims[delim]) {
  118.                 fputs("%-\n", Outf);
  119.                 delim++;
  120.             }
  121.             else
  122.                 fputs("%%\n", Outf);
  123.         (void) fseek(Inf, *seekpts, 0);
  124.         while ((c = getc(Inf)) != '\0')
  125.             putc(c, Outf);
  126.     }
  127. }
  128.